﻿using ConfExpert.Utility;
using ConfExpert.Utility.Grid;
using ConfOrderDS.AppDSTableAdapters;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using DevTeam;
using Extensions;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfOrder
{
    public partial class Styles : XForm
    {
        COStyleTableAdapter ta = new COStyleTableAdapter();

        public Styles()
        {
            InitializeComponent();
        }

        BindingSource bsStyle;
        private void Style_Load(object sender, EventArgs e)
        {
            RepositoryManager.dsi.COStyle.Clear();
            AppSession.DataManager.Fill(RepositoryManager.dsi, "COStyle", "COGetStyles", AppSession.UserInfo.CustomerId);
            bsStyle = new BindingSource(RepositoryManager.dsi, "COStyle");

            InitGridView(gvStyle, bsStyle);
            gvStyle.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            RepositoryManager.dsi.COStyle.TableNewRow += COStyle_TableNewRow;
            gvStyle.Columns["BrandId"].ColumnEdit = RepositoryManager.riBrand;
            gvStyle.SetCaptions("BrandId:Brand");
            gvStyle.MakeColumnInvisible("CustomerId", "Trim5Pos");
            gvStyle.CellValueChanged += gvStyle_CellValueChanged;
            MainToolbar.Visible = true;

            this.Deactivate += Styles_Deactivate;

            GridManager gm = GridManager.GetManager(gvStyle);
            gm.GridMenu.AddMenu("Delete Selected Styles", DelStyles);
            gvStyle.AddSummary();
            gvStyle.BestFitColumns();

            gvStyle.OptionsView.EnableAppearanceOddRow = true;
        }

        void DelStyles(object sender, EventArgs e)
        {
            gvStyle.CloseEditor();
            bsStyle.EndEdit();

            DataTable d = AppSession.DataManager.ExecuteDataTable("CODelStyles", gvStyle.GetSelectedValues("COStyleId"), AppSession.UserInfo.CustomerId);
            RepositoryManager.dsi.COStyle.Clear();
            RepositoryManager.dsi.COStyle.Merge(d);
            gvStyle.RefreshData();
        }

        void Styles_Deactivate(object sender, EventArgs e)
        {
            gvStyle.CloseEditor();
            bsStyle.EndEdit();
            DataTable dt = RepositoryManager.dsi.COStyle.GetChanges();
            if (dt != null)
                bbiSave.PerformClick();
        }

        void gvStyle_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            gvStyle.BestFitColumns();
        }

        void COStyle_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["CustomerId"] = AppSession.UserInfo.CustomerId;
        }

        private void bbiSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvStyle.CloseEditor();
            bsStyle.EndEdit();

            DataTable d = RepositoryManager.dsi.COStyle;

            if (RepositoryManager.dsi.COStyle.Select("Style is null").Length > 0)
            {
                XtraMessageBox.Show("Style can not be empty.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (RepositoryManager.dsi.COStyle.Select("BrandId is null").Length > 0)
            {
                XtraMessageBox.Show("Brand can not be empty.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string style = RepositoryManager.dsi.COStyle.SerializeChanges();
            //if (style != "")
            //    AppSession.DataManager.ExecuteNonQuery("COInsLog", "Style", style);

            ta.Update(RepositoryManager.dsi.COStyle);
            Notify("Saved successfully.");
        }
    }
}